#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
import swmixer
import numpy
from os.path import join
from time import sleep

consonnes = {u"ʃ".encode("utf-8"):{"chaud":"chaud.wav",
                                   "anchois":"anchois.wav",
                                   "short":"short.wav"},
    "b":{"bombons":"bonbons.wav",
          "abricot":"abricot.wav",
          "chambre":"chambre.wav"},
    "k":{u"café":"cafe.wav",
          "sucre":"sucre.wav",
          "psychologie":"psychologie.wav"},
    "d":{"douane":"douane.wav",
          "dinde":"dinde.wav"},
    "f":{u"février":"fevrier.wav",
          "neuf":"neuf.wav",
          "pharmacie":"pharmacie.wav"},
    "g":{"gants":"gants.wav",
          "bague":"bague.wav",
          "gris":"gris.wav"},
    u"ʒ".encode("utf-8"):{"agneau":"agneau.wav",
          "baignore":"baignore.wav"},
    "l":{"lampe":"lampe.wav",
          "fleurs":"fleurs.wav",
          "mille":"mille.wav"},
    "m":{u"mère":"mere.wav",
          "comment":"comment.wav"},
    "n":{"noir":"noir.wav",
          "sonner":"sonner.wav"},
    "d":{"douane":"douane.wav",
          "dinde":"dinde.wav"},
    u"ɲ".encode("utf-8"):{"smoking":"smoking.wav"},
    }
voyelles = {"":{"":""}}
voyellesNasales = {"":{"":""}}
semiVoyelles = {"":{"":""}}

class Sound(wx.Panel):
    def __init__(self, parent, id, word, filename):
        wx.Panel.__init__(self, parent, id)
        self.word = word
        self.filename = filename
        
        soundBox = wx.BoxSizer(wx.HORIZONTAL)
        txt = wx.StaticText(self, -1, word)
        playBtn = wx.BitmapButton(self, -1, images["play"])
        recBtn = wx.BitmapButton(self, -1, images["rec"])
        self.playRec = wx.BitmapButton(self, -1, images["playRec"])
        self.playRec.SetBitmapDisabled(images["playRec_disabled"])
        self.playRec.Enable(False)
        
        self.Bind(wx.EVT_BUTTON, self.onPlay, playBtn)
        self.Bind(wx.EVT_BUTTON, self.onRec, recBtn)
        self.Bind(wx.EVT_BUTTON, self.onPlayRec, self.playRec)
        
        soundBox.Add(txt)
        soundBox.Add(playBtn)
        soundBox.Add(recBtn)
        soundBox.Add(self.playRec)
        
        self.SetSizer(soundBox)
        
        self.recBuffer = None
    
    def onPlay(self, evt):
        swmixer.Sound(join("sons",self.filename)).play()
        swmixer.tick()
    
    def onRec(self, evt):
        frame=0
        micdata = []
        maxFrames = 20
        dlg = wx.ProgressDialog("Enregistrement",
                               "",
                               maximum = maxFrames+1,
                               parent=self,
                               style = wx.PD_APP_MODAL|wx.PD_SMOOTH)
        while frame < maxFrames:
            frame += 1
            sleep(0.1)
            dlg.Update(frame)
            micdata = numpy.append(micdata, swmixer.get_microphone())
        dlg.Destroy()
        self.recBuffer = swmixer.Sound(data=micdata)
        self.playRec.Enable(True)
        evt.Skip()

    def onPlayRec(self, evt):
        if self.recBuffer:
            self.recBuffer.play()



class FrHatsuon(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, -1, title=u"Prononciation française")
        
        swmixer.init(samplerate=11025, chunksize=1024, stereo=False, microphone=True)
        self._buildGUI()
        
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)
        self.timer.Start(100)
        
        self.Show(True)
        
    def onTimer(self, evt):
        swmixer.tick()
        
    def _buildGUI(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        
        book = wx.Notebook(self, -1, style=wx.BK_TOP)
        
        book.AddPage(IPASymbolPanel(book, -1, consonnes), "Consonnes")
        book.AddPage(IPASymbolPanel(book, -1, voyelles), "Voyelles")
        book.AddPage(IPASymbolPanel(book, -1, voyellesNasales), "Voyelles nasales")
        book.AddPage(IPASymbolPanel(book, -1, semiVoyelles), "Semi-voyelles")
        
        sizer.Add(book, flag=wx.EXPAND)
        self.SetSizerAndFit(sizer)

class IPASymbolPanel(wx.Panel):
    def __init__(self, parent, id, examples):
        wx.Panel.__init__(self, parent, id)#, size=(300, 200))
        sizer = wx.BoxSizer(wx.VERTICAL)
        for ex in examples:
            box_title = wx.StaticBox(self, -1, "%s"%ex)
            box = wx.StaticBoxSizer(box_title, wx.VERTICAL)
            for mot in examples[ex]:
                box.Add(Sound(self, -1, mot, examples[ex][mot]))
            sizer.Add(box, flag=wx.EXPAND)
        self.SetSizer(sizer)

    

if __name__=="__main__":
    app = wx.App()
    app.RestoreStdio()
    from images import images
    FrHatsuon()
    app.MainLoop()